package com.wx.core.process;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.wx.core.vo.Article;
import com.wx.core.vo.Image;
import com.wx.core.vo.MsgNews;
import com.wx.core.vo.MsgRequest;
import com.wx.core.vo.MsgResponseImage;
import com.wx.core.vo.MsgResponseNews;
import com.wx.core.vo.MsgResponseText;
import com.wx.core.vo.MsgResponseVoice;
import com.wx.core.vo.MsgText;
import com.wx.core.vo.Voice;

/**
 * 微信 服务处理，消息转换等
 * 建议重写，这里用另外一个对象去创建另外一个，没必要
 */
@Deprecated
public class WxServiceProcess {
	//获取 MsgResponseText 对象
	public static MsgResponseText getMsgResponseText(MsgRequest msgRequest,MsgText msgText){
		if(msgText != null){
			MsgResponseText reponseText = new MsgResponseText();
			reponseText.setToUserName(msgRequest.getFromUserName());
			reponseText.setFromUserName(msgRequest.getToUserName());
			reponseText.setMsgType(MsgType.Text.toString());
			reponseText.setCreateTime(System.currentTimeMillis());
			reponseText.setContent(msgText.getContent());
			return reponseText;
		}else{
			return null;
		}
	}
	
	public static MsgResponseImage getMsgResponseImage(MsgRequest msgRequest,Image image){
		if(image != null){
			MsgResponseImage responseImage = new MsgResponseImage();
			responseImage.setToUserName(msgRequest.getFromUserName());	
			responseImage.setFromUserName(msgRequest.getToUserName());
			responseImage.setMsgType(MsgType.Image.toString());
			responseImage.setCreateTime(new Date().getTime());
			responseImage.setImage(image);
			return responseImage;
			}
		return null;
		
	}
	
	//获取 MsgResponseText 对象
	public static MsgResponseVoice getMsgResponseVoice(MsgRequest msgRequest,String mediaId){
		if(!StringUtils.isEmpty(mediaId)){
			MsgResponseVoice reponseVoice = new MsgResponseVoice();
			reponseVoice.setToUserName(msgRequest.getFromUserName());
			reponseVoice.setFromUserName(msgRequest.getToUserName());
			reponseVoice.setMsgType(MsgType.Voice.toString());
			reponseVoice.setCreateTime(new Date().getTime());
			Voice voice = new Voice();
			voice.setMediaId(mediaId);
			reponseVoice.setVoice(voice);
			return reponseVoice;
		}else{
			return null;
		}
	}
	
	//获取 MsgResponseNews 对象
	public static MsgResponseNews getMsgResponseNews(MsgRequest msgRequest,List<MsgNews> msgNews){
		if(msgNews != null && msgNews.size() > 0){
			MsgResponseNews responseNews = new MsgResponseNews();
			responseNews.setToUserName(msgRequest.getFromUserName());
			responseNews.setFromUserName(msgRequest.getToUserName());
			responseNews.setMsgType(MsgType.News.toString());
			responseNews.setCreateTime(new Date().getTime());
			responseNews.setArticleCount(msgNews.size());
			List<Article> articles = new ArrayList<Article>(msgNews.size());
			for(MsgNews n : msgNews){
				Article a = new Article();
				a.setTitle(n.getTitle());
				a.setPicUrl(n.getPicpath());
				if(StringUtils.isEmpty(n.getFromurl())){
					a.setUrl(n.getUrl());
				}else{
					a.setUrl(n.getFromurl());
				}
				a.setDescription(n.getBrief());
				articles.add(a);
			}
			responseNews.setArticles(articles);
			return responseNews;
		}else{
			return null;
		}
	}
	
}